package com.sakura.mall.member.memberLoginLog.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.member.memberLoginLog.domain.entity.MemberLoginLogEntity;
import com.sakura.mall.member.memberLoginLog.domain.request.AddMemberLoginLogRequest;
import com.sakura.mall.member.memberLoginLog.domain.request.UpdateMemberLoginLogRequest;
import com.sakura.mall.member.memberLoginLog.domain.request.MemberLoginLogPageRequest;
import com.sakura.mall.member.memberLoginLog.domain.vo.MemberLoginLogVo;
import com.sakura.mall.member.memberLoginLog.mapper.MemberLoginLogMapper;
import com.sakura.mall.member.memberLoginLog.service.MemberLoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员登录记录 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class MemberLoginLogServiceImpl extends ServiceImpl<MemberLoginLogMapper, MemberLoginLogEntity> implements MemberLoginLogService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddMemberLoginLogRequest request) {
        // todo 其他校验逻辑
        MemberLoginLogEntity memberLoginLog = request.convert(MemberLoginLogEntity.class);
        this.save(memberLoginLog);
    }

    @Override
    public Page<MemberLoginLogVo> queryPage(MemberLoginLogPageRequest request) {
        IPage<MemberLoginLogEntity> qp = request.toPage();
        LambdaQueryWrapper<MemberLoginLogEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<MemberLoginLogVo> ret = qp.convert(entity -> entity.convert(MemberLoginLogVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateMemberLoginLogRequest request) {
        MemberLoginLogEntity entity = request.convert(MemberLoginLogEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

